package action;

import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Disciplina;

public class ExcluirDisciplinaAction extends ManterDisciplinaAction {

    @Override
    public void setParametros(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        try {
            
            request.setAttribute("editar", "readonly");
            request.setAttribute("excluir", "readonly");
            
            int codDisciplina = Integer.parseInt(request.getParameter("codDisciplina"));
            Disciplina disciplina = Disciplina.obterDisciplina(codDisciplina);
            request.setAttribute("disciplina", disciplina);
            String preRequisitosSerialized = "";
            for (Disciplina preRequisito : disciplina.getDisciplinasForCodDisciplinaPreRequisito()) {
                preRequisitosSerialized = preRequisitosSerialized + preRequisito.getCodDisciplina() + ",";
            }
            request.setAttribute("preRequisitosSerialized", preRequisitosSerialized);
            
        } catch (SQLException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        }
    }

    @Override
    public void efetuaPersistencia(Disciplina disciplina) throws ServletException {
        try{
            disciplina.excluir();
        } catch (SQLException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        }
    }

}
